package com.cloud.pole.common.msg;

import java.util.HashMap;
import java.util.Map;

import com.cloud.pole.common.constant.CommonConstants;

/**
 * 对象响应结果
 * ps：
 * {
 * "status": 1, 正数为成功，负数为失败
 * "message": "操作成功",
 * "data": {}
 * }
 */
public class ApiRestResponse<T> extends BaseResponse {

    private T data;     //返回数据

    private ApiRestResponse() {
    }
  
    private ApiRestResponse(int status, String message, T data) {
        super(status, message);
        this.data = data;
    }

    /**
     * 成功
     *
     * @return
     */
    public static <T> ApiRestResponse<T> success(int status, String message, T data) {
        return new ApiRestResponse<>(status, message, data);
    }

    /**
     * 成功
     *
     * @return
     */
    public static <T> ApiRestResponse<T> success(String message, T data) {
        return new ApiRestResponse<>(CommonConstants.STATUS_OK_CODE, message, data);
    }

    /**
     * 成功数据
     * @param message
     * @param total
     * @param page
     * @param limit
     * @param data 
     * @return
     */
    public static ApiRestResponse successOfPage(String message, int totalPage, int currentPage, int pageSize, Object data) {
        Map map = new HashMap();
        map.put("page", currentPage);
        map.put("limit", pageSize);
        map.put("total", totalPage);
        map.put("rows", data);
        return new ApiRestResponse(CommonConstants.STATUS_OK_CODE, message, map);
    }

    /**
     * 失败
     *
     * @return
     */
    public static <T> ApiRestResponse<T> fail(int status, String message, T data) {
        return new ApiRestResponse<>(status, message, data);
    }

    /**
     * 失败
     *
     * @return
     */
    public static <T> ApiRestResponse<T> fail(String message, T data) {
        return new ApiRestResponse<>(CommonConstants.STATUS_ERROR_CODE, message, data);
    }

    /**
     * 无返回值情况操作成功或失败
     *
     * @return
     */
    public static <T> ApiRestResponse over(boolean condition) {
        if (condition) {
            return ApiRestResponse.success("操作成功", null);
        }
        return ApiRestResponse.fail("操作失败", null);
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
